package com.anlu.base.database;

import com.anlu.base.UserAccount;
import com.anlu.base.constants.CachingConstants;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClientFactory;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;

import static com.anlu.base.constants.CachingConstants.*;

@Slf4j
public class MongoDb implements DbManager{

    private static final String DATABASE_NAME = "admin";
    private static final String MONGO_USER = "root";
    private static final String MONGO_PASSWORD = "rootpassword";
    private MongoClient client;
    private MongoDatabase db;

    void setDb(MongoDatabase db){
        this.db = db;
    }

    @Override
    public void connect() {
        // 构建连接字符串，包含服务器地址、端口、用户名、密码等信息（按实际替换）
//        String connectionString = "mongodb://username:password@remote_server_address:27017/mydb";
//        MongoClientURI uri = new MongoClientURI(connectionString);
//
//        // 根据连接字符串创建MongoClient实例
//        MongoClient mongoClient = new MongoClient(uri);
//
//        // 获取指定的数据库（这里假设数据库名为'mydb'）
//        MongoDatabase database = mongoClient.getDatabase("mydb");
//
//        // 后续进行相应操作，最后关闭连接
//        mongoClient.close();
    }

    @Override
    public void disconnect() {

        client.close();
    }

    @Override
    public UserAccount readFromDb(String userId) {
        var iterable = db
                .getCollection(CachingConstants.USER_ACCOUNT)
                .find(new Document(USER_ID, userId));
        if (iterable.first() == null) {
            return null;
        }
        Document doc = iterable.first();
        if (doc != null) {
            String userName = doc.getString(USER_NAME);
            String appInfo = doc.getString(ADD_INFO);
            return new UserAccount(userId, userName, appInfo);
        } else {
            return null;
        }
    }

    @Override
    public UserAccount writeToDb(UserAccount userAccount) {
        db.getCollection(USER_ACCOUNT).insertOne(
                new Document(USER_ID, userAccount.getUserID())
                        .append(USER_NAME, userAccount.getUserName())
                        .append(ADD_INFO, userAccount.getAdditionalInfo())
        );
        return userAccount;
    }

    @Override
    public UserAccount updateDb(UserAccount userAccount) {
        Document id = new Document(USER_ID, userAccount.getUserID());
        Document dataSet = new Document(USER_NAME, userAccount.getUserName())
                .append(ADD_INFO, userAccount.getAdditionalInfo());
        db.getCollection(CachingConstants.USER_ACCOUNT)
                .updateOne(id, new Document("$set", dataSet));
        return userAccount;
    }

    @Override
    public UserAccount upsertDb(UserAccount userAccount) {
        String userId = userAccount.getUserID();
        String userName = userAccount.getUserName();
        String additionalInfo = userAccount.getAdditionalInfo();
        db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
                new Document(USER_ID, userId),
                new Document("$set",
                        new Document(USER_ID, userId)
                                .append(USER_NAME, userName)
                                .append(ADD_INFO, additionalInfo)
                ),
                new UpdateOptions().upsert(true)
        );
        return userAccount;
    }
}
